package com.winona.ui;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.json.JSONException;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.taplinker.client.R;
import com.taplinker.client.core.ImageManager;
import com.taplinker.client.im.activity.ChatActivity;
import com.taplinker.core.rpc.Result;
import com.taplinker.core.rpc.command.SimpleBaseCommand;
import com.taplinker.core.rpc.http.HttpTemplate;
import com.taplinker.core.util.json.JsonUtil;
import com.winona.Base1Activity;
import com.winona.ConstantValue;
import com.winona.bean.ProductBean;

public class ProductActivity extends Base1Activity {
	private ImageButton back;
	private TextView title, pro;
	private List<ProductBean> productList;
	private BaseAdapter mAdapter;
	private GridView mListView;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.send_pro);

		findView();
		setLinstener();
		getData();
	}

	private void getData() {
		new SimpleBaseCommand() {

			@Override
			public Result go() {

				return new HttpTemplate().exchange(ConstantValue.COMMON
						+ "/product/finds?gotopage=1&pagesize=20");
			}

			@Override
			public void yes(Result result) {

				String json = result.object.toString();
				if (StringUtils.isNotEmpty(json)) {
					try {
						org.json.JSONObject jo = new org.json.JSONObject(json);
						String jstr = null;
						jstr = jo.getString("body");
						List<ProductBean> list = JsonUtil.jsonToList(jstr,
								ProductBean.class);
						productList.addAll(list);
					} catch (JSONException e) {

						e.printStackTrace();
					}
				}
				if (mAdapter == null) {
					mAdapter = new ProductAdapter();
					mListView.setAdapter(mAdapter);
				} else {
					mAdapter.notifyDataSetChanged();
				}
			}

			@Override
			public void no(Result result) {

				super.no(result);
			}

		}.execute();

	}

	private void setLinstener() {
		back.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				startActivity(ChatActivity.class);

			}
		});

	}

	private void findView() {
		back = (ImageButton) findViewById(R.id.send_back);
		title = (TextView) findViewById(R.id.send_title);
		pro = (TextView) findViewById(R.id.send_pro);
		mListView = (GridView) findViewById(R.id.pro_listView);
		productList = new ArrayList<ProductBean>();

	}

	private final class ProductAdapter extends BaseAdapter {

		@Override
		public int getCount() {
			return productList.size();
		}

		@Override
		public Object getItem(int position) {
			return productList.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder vHolder = null;
			if (convertView == null) {
				convertView = LayoutInflater.from(ProductActivity.this)
						.inflate(R.layout.send_list, null);
				vHolder = new ViewHolder();
				vHolder.picture = (ImageView) convertView
						.findViewById(R.id.sent_img);
				vHolder.name = (TextView) convertView
						.findViewById(R.id.pro_text);

				convertView.setTag(vHolder);
			} else {
				vHolder = (ViewHolder) convertView.getTag();
			}

			final ProductBean product = (ProductBean) getItem(position);

			ImageManager.getInstance().loadImage(
					ConstantValue.IMAGE + productList.get(position).getImage(),
					vHolder.picture, 60, 60, null);

			vHolder.name.setText(product.getName());

			return convertView;
		}

		private final class ViewHolder {
			ImageView picture;
			TextView name;

		}

	}

}
